﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SistemaAcademicoBO.Entities;
using SistemaAcademicoBLL;
using SistemaAcademicoBLL.Util;
using System.Collections;
using SistemaAcademicoBO;

namespace SistemaAcademico.Pages
{
    public partial class NewSubject : System.Web.UI.Page
    {
        #region FIELDS
        string subjectName;
        int cargaHoraria;
        ApplicationFacade facade = new ApplicationFacade();
        Subject subject;
        int subjectId = 0;
        #endregion

        #region PAGE EVENTS
        protected void Page_Load(object sender, EventArgs e)
        {
            #region EVENTS
            btnCadastrar.Click += new EventHandler(btnCadastrar_Click);
            #endregion

            if (!IsPostBack)
            {
                lstProfessores = UsefullStuffs.LoadProfessors(lstProfessores);
            }
           

            #region QueryString
            String action = Request.QueryString["act"];
            if (action == "edit")
            {
                subjectId = Convert.ToInt32(Request.QueryString["id"]);
                subject = facade.GetSubject(subjectId);
                txtCargaHoraria.Text = subject.CargaHoraria.ToString();
                txtSubjectName.Text = subject.SubjectName;

                #region Load ProfessorsSubjects

                if (!IsPostBack)
                {
                    lstPRofessoresHabilitados = UsefullStuffs.LoadProfessorSubjectBySubjectId(lstPRofessoresHabilitados, subjectId);
                    foreach (ListItem listItem in lstPRofessoresHabilitados.Items)
                    {
                        lstProfessores.Items.Remove(listItem);
                    } 
                }
                #endregion
            }
            #endregion
        }
        #endregion

        #region CONTROL EVENTS
        void btnCadastrar_Click(object sender, EventArgs e)
        {
            if ((String.IsNullOrEmpty(txtSubjectName.Text) || (String.IsNullOrEmpty(txtCargaHoraria.Text))))
            {
                Alert.Show("Digite as informações.");
            }
            else
            {
                subjectName = txtSubjectName.Text;
                cargaHoraria = Convert.ToInt32(txtCargaHoraria.Text);
                try
                {
                    subject = new Subject(0, cargaHoraria, subjectName);
                    if (subjectId != 0)
                    {
                        subject.SubjectId = subjectId;

                        //REMOVE TODOS OS professorsSubjects
                        List<ProfessorSubject> allProfessorsSubject = facade.GetAllProfessorsSubjectsBySubjectId(subjectId);
                        foreach (ProfessorSubject professorSubject in allProfessorsSubject)
                        {
                            facade.RemoveProfessorSubject(professorSubject.ProfessorId, professorSubject.SubjectId);
                        }
                    }
                    facade.SetSubject(subject);
                    int identCurrent = facade.GetIdentCurrent("Subjects");

                    Subject subjectIdent = facade.GetSubject(identCurrent);
                    if (subjectIdent.SubjectName == subject.SubjectName)
                    {
                        foreach (ListItem listItem in lstPRofessoresHabilitados.Items)
                        {
                            facade.SetProfessorSubject(new ProfessorSubject(Convert.ToInt32(listItem.Value), subjectIdent.SubjectId));
                        }
                    }
                    Alert.Show("Cadastro realizado com sucesso.");
                }
                catch
                {
                    Alert.Show("Houve um erro no cadastro.");
                }
            }
        }
        protected void lbtnAddProfessor_Click(object sender, EventArgs e)
        {
            UsefullStuffs.TransferListItem(ref lstProfessores, ref lstPRofessoresHabilitados);
        }

        protected void lbtnRemoveProfessor_Click(object sender, EventArgs e)
        {
            UsefullStuffs.TransferListItem(ref lstPRofessoresHabilitados, ref lstProfessores);
        }

        #endregion

        #region METHODS

        #endregion



    }
}